package com.rabbiter.oes.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.rabbiter.oes.dto.RePassDTO;
import com.rabbiter.oes.dto.RegisterDTO;
import com.rabbiter.oes.entity.Admin;
import com.rabbiter.oes.entity.Student;
import com.rabbiter.oes.entity.Teacher;
import com.rabbiter.oes.enums.RoleEnum;
import com.rabbiter.oes.mapper.AdminMapper;
import com.rabbiter.oes.mapper.StudentMapper;
import com.rabbiter.oes.mapper.TeacherMapper;
import com.rabbiter.oes.result.R;
import com.rabbiter.oes.service.LoginService;
import com.rabbiter.oes.utils.PasswordEncryptionExample;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * 登录相关service
 */
@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private StudentMapper studentMapper;


    @Override
    public RoleEnum getRole(String uname) {
        Admin admin = adminMapper.selectByAccAndPwd(uname);
        if (admin != null) {
            return RoleEnum.ADMIN;
        }

        Teacher teacher = teacherMapper.selectByAccAndPwd(uname);
        if (teacher != null) {
            return RoleEnum.TEACHER;
        }

        Student student = studentMapper.selectByAccAndPwd(uname);
        if (student != null) {
            return RoleEnum.STUDENT;
        }
        return RoleEnum.UNKNOWN;
    }

    @Override
    public Admin adminLogin(String username, String password) {
        // 数据合法性验证
        Assert.isTrue(ObjectUtil.isNotEmpty(username), "账号不允许为空");
        Assert.isTrue(ObjectUtil.isNotEmpty(password), "密码不允许为空");
        Admin admin = adminMapper.selectByAccAndPwd(username);
        Assert.isTrue(ObjectUtil.isNotEmpty(admin), "账号不存在");
        Assert.isTrue(PasswordEncryptionExample.checkPassword(password, admin.getPwd()), "密码错误，请重新输入！");
        return admin;
    }

    @Override
    public Teacher teacherLogin(String username, String password) {
        // 数据合法性验证
        Assert.isTrue(ObjectUtil.isNotEmpty(username), "账号不允许为空");
        Assert.isTrue(ObjectUtil.isNotEmpty(password), "密码不允许为空");
        Teacher teacher = teacherMapper.selectByAccAndPwd(username);
        Assert.isTrue(ObjectUtil.isNotEmpty(teacher), "账号不存在");
        Assert.isTrue(PasswordEncryptionExample.checkPassword(password, teacher.getPwd()), "密码错误，请重新输入！");
        return teacher;
    }

    @Override
    public Student studentLogin(String username, String password) {
        // 数据合法性验证
        Assert.isTrue(ObjectUtil.isNotEmpty(username), "账号不允许为空");
        Assert.isTrue(ObjectUtil.isNotEmpty(password), "密码不允许为空");
        Student student = studentMapper.selectByAccAndPwd(username);
        Assert.isTrue(ObjectUtil.isNotEmpty(student), "账号不存在");
        Assert.isTrue(PasswordEncryptionExample.checkPassword(password, student.getPwd()), "密码错误，请重新输入！");
        return student;
    }


    @Override
    public R repass(RePassDTO rePassDTO) {
        // 用户名
        String username = rePassDTO.getUsername();
        // 手机号
        String tel = rePassDTO.getTel();
        // email
        String email = rePassDTO.getEmail();
        // 密码
        String password = rePassDTO.getPassword();
        // 身份证号
        String cardId = rePassDTO.getCardId();
        Assert.isTrue(ObjectUtil.isNotEmpty(username), "账号不允许为空");
        Assert.isTrue(ObjectUtil.isNotEmpty(tel), "手机号不允许为空");
        Assert.isTrue(ObjectUtil.isNotEmpty(email), "电子邮件不允许为空");
        Assert.isTrue(ObjectUtil.isNotEmpty(password), "密码不允许为空");
        Assert.isTrue(ObjectUtil.isNotEmpty(cardId), "身份证号不允许为空");

        // 加密前端传入的密码
        password = PasswordEncryptionExample.encrypted(password);

        // 执行修改密码逻辑
        Admin admin = adminMapper.selectByAccAndPwd(username);
        Teacher teacher = teacherMapper.selectByAccAndPwd(username);
        Student student = studentMapper.selectByAccAndPwd(username);

        if (admin != null && tel.equals(admin.getTel()) && email.equals(admin.getEmail()) && cardId.equals(admin.getCardId())) {
            int i = adminMapper.updatePwdById(admin.getId(), password);
            if (i > 0) {
                return R.ok("找回管理员账号成功");
            }
        } else if (teacher != null && tel.equals(teacher.getTel()) && email.equals(teacher.getEmail()) && cardId.equals(teacher.getCardId())) {
            int i = teacherMapper.updatePwdById(teacher.getId(), password);
            if (i > 0) {
                return R.ok("找回教师账号成功");
            }
        } else if (student != null && tel.equals(student.getTel()) && email.equals(student.getEmail()) && cardId.equals(student.getCardId())) {
            int i = studentMapper.updatePwdById(student.getId(), password);
            if (i > 0) {
                return R.ok("找回学生账号成功");
            }
        }
        return R.logicError("找回密码失败，请检查输入数据");
    }

    @Override
    public R register(RegisterDTO registerDTO) {

        // 账号
        String acc = registerDTO.getAcc();
        Assert.isTrue(ObjectUtil.isNotEmpty(acc), "账号不允许为空");
        // 姓名
        String name = registerDTO.getName();
        Assert.isTrue(ObjectUtil.isNotEmpty(name), "姓名不允许为空");
        // 手机号
        String tel = registerDTO.getTel();
        Assert.isTrue(ObjectUtil.isNotEmpty(tel), "手机号不允许为空");
        // 电子邮件
        String email = registerDTO.getEmail();
        Assert.isTrue(ObjectUtil.isNotEmpty(email), "电子邮件不允许为空");
        // 电子邮件
        String sex = registerDTO.getSex();
        Assert.isTrue(ObjectUtil.isNotEmpty(sex), "性别不允许为空");
        // 密码
        String password = registerDTO.getPassword();
        Assert.isTrue(ObjectUtil.isNotEmpty(password), "密码不允许为空");
        // 身份证号
        String cardId = registerDTO.getCardId();
        Assert.isTrue(ObjectUtil.isNotEmpty(cardId), "身份证号不允许为空");
        // 角色
        String role = registerDTO.getRole();
        Assert.isTrue(ObjectUtil.isNotEmpty(role), "角色不允许为空");

        // 加密前端传入的密码
        password = PasswordEncryptionExample.encrypted(password);

        Admin admindb = adminMapper.selectByAccAndPwd(acc);
        Teacher teacherdb = teacherMapper.selectByAccAndPwd(acc);
        Student studentdb = studentMapper.selectByAccAndPwd(acc);
        if (admindb != null && teacherdb != null && studentdb != null) {
            return R.error("该账号已存在");
        }


        if (role.equals(String.valueOf(RoleEnum.TEACHER.getCode()))) {
            Teacher teacher = new Teacher();
            teacher.setAcc(acc);
            teacher.setName(name);
            teacher.setSex(sex);
            teacher.setTel(tel);
            teacher.setEmail(email);
            teacher.setPwd(password);
            teacher.setCardId(cardId);
            teacher.setRole(role);
            int insert = teacherMapper.insert(teacher);
            if (insert > 0) {
                return R.ok("教师注册成功");
            }
        } else if (role.equals(String.valueOf(RoleEnum.STUDENT.getCode()))) {
            Student student = new Student();
            student.setAcc(acc);
            student.setName(name);
            student.setSex(sex);
            student.setTel(tel);
            student.setEmail(email);
            student.setPwd(password);
            student.setCardId(cardId);
            student.setRole(role);
            int insert = studentMapper.insert(student);
            if (insert > 0) {
                return R.ok("学生注册成功");
            }
        }
        return R.logicError("注册失败");
    }
}
